package mv.crawler;

import com.google.gdata.client.calendar.CalendarQuery;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.calendar.CalendarFeed;
import com.google.gdata.data.calendar.CalendarEntry;
import mv.io.SimpleProperties;
import mv.wap.Page;
import mv.wap.Paragraph;
import mv.wap.Link;

import java.net.URL;
import java.util.Calendar;
import java.util.List;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.TimeZone;
import java.util.ArrayList;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Class description...
 *
 * @author Gantz
 */
public class CalendarCrawler extends GoogleCrawler {
  static DateFormat dateTimeFormat = new SimpleDateFormat("EEE, dd MMM h:mm a");
  static DateFormat dateOnlyFormat = new SimpleDateFormat("EEE, dd MMM");

  static{
    dateOnlyFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  }

  public List<Page> crawl() throws Exception{
    List<CalendarEventEntry> events = getEvents();
    Page page = new Page("Calendar",
            new Link(Page.HOME),
            new Paragraph(dateTimeFormat.format(new Date())));
    for(CalendarEventEntry entry : events){
      DateTime startTime = entry.getTimes().get(0).getStartTime();
      String title = entry.getTitle().getPlainText();
      String time = startTime.isDateOnly() ? dateOnlyFormat.format(startTime.getValue())
        : dateTimeFormat.format(startTime.getValue());
      page.addElement(new Paragraph(time, title));
    }
    return toList(page);
  }

  private List<CalendarEventEntry> getEvents() throws Exception {
    CalendarService myService = new CalendarService("Calendar Service");
    myService.setUserCredentials(SimpleProperties.getUsername(), SimpleProperties.getPassword());

    List<CalendarEventEntry> events = new ArrayList<CalendarEventEntry>();
    Calendar maxDate = Calendar.getInstance();
    maxDate.add(Calendar.MONTH, 2);
    URL allCals = new URL("http://www.google.com/calendar/feeds/default/allcalendars/full");
    CalendarFeed resultFeed = myService.getFeed(allCals, CalendarFeed.class);
    for (int i = 0; i < resultFeed.getEntries().size(); i++) {
      CalendarEntry entry = resultFeed.getEntries().get(i);
      URL allEvents = new URL(entry.getLinks().get(0).getHref());
      CalendarQuery myQuery = new CalendarQuery(allEvents);
      myQuery.setMinimumStartTime(DateTime.now());
      myQuery.setMaximumStartTime(new DateTime(maxDate.getTime()));
      CalendarEventFeed eventsFeed = myService.query(myQuery, CalendarEventFeed.class);
      events.addAll(eventsFeed.getEntries());
    }
    Collections.sort(events, new Comparator<CalendarEventEntry>(){
      public int compare(CalendarEventEntry o1, CalendarEventEntry o2) {
        DateTime startTime1 = o1.getTimes().get(0).getStartTime();
        DateTime startTime2 = o2.getTimes().get(0).getStartTime();
        return startTime1.compareTo(startTime2);
      }
    });
    return events;
  }

  public static void main(String[] args) throws Exception {
    List<Page> pages = new CalendarCrawler().crawl();
  }
}
